package com.ruoyi.car.service.impl;

import com.ruoyi.car.domain.CarGoods;
import com.ruoyi.car.domain.CarTestDrive;
import com.ruoyi.car.mapper.CarTestDriveMapper;
import com.ruoyi.car.service.ICarGoodsService;
import com.ruoyi.car.service.ICarTestDriveService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 试驾管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class CarTestDriveServiceImpl implements ICarTestDriveService {
    @Autowired
    private CarTestDriveMapper carTestDriveMapper;

    @Autowired
    private ICarGoodsService carGoodsService;

    /**
     * 查询试驾管理
     *
     * @param id 试驾管理主键
     * @return 试驾管理
     */
    @Override
    public CarTestDrive selectCarTestDriveById(Long id) {
        return carTestDriveMapper.selectCarTestDriveById(id);
    }

    /**
     * 查询试驾管理列表
     *
     * @param carTestDrive 试驾管理
     * @return 试驾管理
     */
    @Override
    public List<CarTestDrive> selectCarTestDriveList(CarTestDrive carTestDrive) {
        return carTestDriveMapper.selectCarTestDriveList(carTestDrive);
    }

    /**
     * 新增试驾管理
     *
     * @param carTestDrive 试驾管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCarTestDrive(CarTestDrive carTestDrive) {
        this.beforeSave(carTestDrive);
        if (carTestDrive.getStatus() == null) {
            carTestDrive.setStatus("预约中");
        }
        checkDry(carTestDrive);
        carTestDrive.setCreateTime(DateUtils.getNowDate());
        return carTestDriveMapper.insertCarTestDrive(carTestDrive);
    }

    /**
     * 修改试驾管理
     *
     * @param carTestDrive 试驾管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateCarTestDrive(CarTestDrive carTestDrive) {
        this.beforeSave(carTestDrive);
        carTestDrive.setUpdateTime(DateUtils.getNowDate());
        return carTestDriveMapper.updateCarTestDrive(carTestDrive);
    }

    private void beforeSave(CarTestDrive carTestDrive) {
        if (carTestDrive.getGoodsId() != null) {
            CarGoods carGoods = carGoodsService.selectCarGoodsById(carTestDrive.getGoodsId());
            if (carGoods == null) {
                throw new ServiceException("所选车辆查不到");
            }
            carTestDrive.setCar(carGoods.getGoodsName());
        }
    }

    private void checkDry(CarTestDrive carTestDrive) {
        String phone = carTestDrive.getPhone();
        CarTestDrive query = new CarTestDrive();
        query.setPhone(phone);
        query.setGoodsId(carTestDrive.getGoodsId());
        List<CarTestDrive> carTestDrives = carTestDriveMapper.selectCarTestDriveList(query);
        if (CollectionUtils.isNotEmpty(carTestDrives)) {
            CarTestDrive oldDry = carTestDrives.get(0);
            if (oldDry.getStatus().equals("预约中")) {
                throw new ServiceException("已成功预约待试驾");
            } else {
                throw new ServiceException("当前手机号用户已经试驾完成，请勿再预约");
            }
        }
    }

    /**
     * 批量删除试驾管理
     *
     * @param ids 需要删除的试驾管理主键
     * @return 结果
     */
    @Override
    public int deleteCarTestDriveByIds(Long[] ids) {
        return carTestDriveMapper.deleteCarTestDriveByIds(ids);
    }

    /**
     * 删除试驾管理信息
     *
     * @param id 试驾管理主键
     * @return 结果
     */
    @Override
    public int deleteCarTestDriveById(Long id) {
        return carTestDriveMapper.deleteCarTestDriveById(id);
    }

    @Override
    public int updateCarTestDriveGoodsName(String goodsName, Long goodsId) {
        return carTestDriveMapper.updateCarTestDriveGoodsName(goodsName, goodsId);
    }

    @Override
    public int countAll() {
        return carTestDriveMapper.countAll();
    }
}
